package cn.jisol.ngame.demo.game.action.unity.service;

import cn.jisol.ngame.demo.client.UnityFrameNClient;
import cn.jisol.ngame.ncall.NCallServiceImpl;
import cn.jisol.ngame.rpc.NGameRPCClass;
import cn.jisol.ngame.rpc.NGameRPCMethod;
import cn.jisol.ngame.rpc.NRPCParam;
import cn.jisol.ngame.sync.NFPSInfo;
import cn.jisol.ngame.sync.fps.NSyncFPSMethod;
import cn.jisol.ngame.sync.fps.NSyncFPSMode;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Component
@NGameRPCClass
public class SNUnityFrameAction extends NCallServiceImpl {

    //同步nSyncModes
    public NSyncFPSMode<Object> nSyncFPSMode = null;

    //玩家列表
    public Map<String, UnityFrameNClient> clients;

    /**
     * 启动帧同步
     */
    @NGameRPCMethod
    public void nGameSyncStart(){

        if(Objects.nonNull(nSyncFPSMode)) return;

        //创建帧同步对象
        nSyncFPSMode = new NSyncFPSMode<>();
        nSyncFPSMode.setIntervalTime(1000/10); //设置同步帧
        //添加同步功能
        addRegister(nSyncFPSMode);

        //开启同步
        nSyncFPSMode.start();
        System.out.println("SNCocosFrameAction - nGameSyncStart : 开始同步模式");

    }

    /**
     * 同步模式回调
     */
    @NSyncFPSMethod
    public void nGameSyncCallBack(String uuid, NFPSInfo<Object> frame){

        //发送封装好的 向所有client 调用 nGameSyncCallBack
        for (UnityFrameNClient value : clients.values()) {

            value.cNUnityFrameAction.nGameSyncCallBack(frame);

        }

    }

    /**
     * 添加帧操作
     */
    @NGameRPCMethod
    public void addInputs(@NRPCParam("inputs") List<Object> inputs){

        System.out.println("SNUnityFrameAction - addInputs");

        if(Objects.nonNull(this.nSyncFPSMode)){
            this.nSyncFPSMode.addFPSInfos(inputs);
        }

    }

    public void nGameSyncClose() {

        if(Objects.isNull(nSyncFPSMode)) return;

        nSyncFPSMode.end();
        nSyncFPSMode = null;

        System.out.println("SNCocosFrameAction - nGameSyncClose : 关闭帧模式");

    }
}
